//
//  MBCompetationController.m
//  MiBaoTu
//
//  Created by 彭鹏 on 16/9/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBCompetationController.h"
#import "MBTheComCell.h"
#import "MBComDetailController.h"
#import "MBTheComICell.h"
#import "MBLoginController.h"

#import "MBCompetationDetailsController.h"

@interface MBCompetationController ()<UITableViewDelegate,UITableViewDataSource> {
    int                 page;
    NSMutableArray      *_mutArr;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray*dateArr;
@end

@implementation MBCompetationController
-(NSMutableArray*)dateArr{
    if (!_dateArr) {
        _dateArr=[[NSMutableArray alloc]init];
    }return _dateArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    _mutArr = [[NSMutableArray alloc]init];
//    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.title = @"比赛专区";
    [self setupNavBar];
    [self setupUI];
    [self onLoadData];
}

- (void)setupUI {
   [self setRightPlayerItem];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
    [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MBTheComICell class] forCellReuseIdentifier:@"MBTheComICell"];
    
    
}

- (void)setupNavBar {
    
    [self setBackItem];
}

-(UITableView *)tableView {
   
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    }
    return _tableView;
}

#pragma mark - 数据加载
- (void)onLoadData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FAMILYSHOW_ACTIVITYLIST_URL_PATH,[NSNumber numberWithInt:page]] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        
        NSLog(@"json==%@",json);
        if ([json[@"ret_code"] integerValue]==0) {
            NSArray *arr = json[@"data"];
            if (arr.count > 0) {
                if (page == 1) {
                    [_mutArr removeAllObjects];
                }
                [_mutArr addObjectsFromArray:arr];
            }
            [_tableView reloadData];
        } else {
            [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];

}

- (void)onLoadNew
{
    page = 1;
    [self onLoadData];
}

- (void)onLoadMore
{
    ++page;
    [self onLoadData];
}

#pragma mark - UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _mutArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (ISIPAD) {
        MBTheComICell*cell=[tableView dequeueReusableCellWithIdentifier:@"MBTheComICell"];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.bigImageView.image = [UIImage imageNamed:@"bg_dl"];
//        cell.titleLb.text = @"教师节亲子秀活动";
        cell.dataDic=[_mutArr objectAtIndex:indexPath.row];
        return cell;

    }
    MBTheComCell *cell = [MBTheComCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    if (_mutArr.count > indexPath.row) {
        cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (ISIPAD) {
        return 620*PERCEN;
    }
    return 224;//424*PERCEN;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([_mutArr objectAtIndex:indexPath.row]) {
        NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
        
        MBTheComCell *cell = (MBTheComCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSString *StartTime = [dic objectForKey:@"StartTime"];
        NSString *EndTime = [dic objectForKey:@"EndTime"];
        int isActivity = [cell onSolveTimeWithStart:StartTime withEndTimer:EndTime];
        
        if (isActivity == 1) {
            //进行中 判断是否已经参与
            [self onLoadDataForActionStatus:dic];
        } else {
            MBCompetationDetailsController *detail = [[MBCompetationDetailsController alloc]init];
            detail.dataDic = dic;
            detail.isStart = (isActivity == 1 ? YES : NO);
            [self.navigationController pushViewController:detail animated:YES];
        }
    }
}

//判断用户是否已经参与
- (void)onLoadDataForActionStatus:(NSDictionary *)dic
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FAMILYSHOW_ACTIVITY_ACTION_STATUS_URL_PATH,[dic objectForKey:@"ID"],model.UserID] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        MBCompetationDetailsController *detail = [[MBCompetationDetailsController alloc]init];
        detail.dataDic = dic;
        detail.isStart = ([json[@"ret_code"] integerValue]==0 ? NO : YES);
        [self.navigationController pushViewController:detail animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
     
    }];
}

@end
